package com.slkj.shilixiaoyuanapp.ui.main.community;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.CommunityEntity;
import com.slkj.shilixiaoyuanapp.entity.MainToolEntity;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ChosePicAdapter;
import com.slkj.shilixiaoyuanapp.util.ListUtils;
import com.slkj.shilixiaoyuanapp.view.popwindow.CommunityCommentPWindow;

import org.w3c.dom.Comment;

import java.security.interfaces.ECKey;
import java.util.List;

/**
 * Created  on 2018-08-13
 *
 * @author 唐开阔
 * @describe 社区适配器
 */
public class CommunityAdapter extends BaseMultiItemQuickAdapter<CommunityEntity, BaseViewHolder> {
    private Context context;
    private CallBack callBack;
    public CommunityAdapter(List<CommunityEntity> data, Context context) {
        super(data);
        this.context = context;
        addItemType(CommunityEntity.TYPE_COMMON, R.layout.item_community_common);
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    protected void convert(BaseViewHolder helper, CommunityEntity item) {
        switch (helper.getItemViewType()) {
            case CommunityEntity.TYPE_COMMON:
                bindCommonData(helper, item);
                break;
        }
    }

    /**
     * 绑定通用类型数据
     *
     * @param helper
     * @param item
     */
    private void bindCommonData(BaseViewHolder helper, CommunityEntity item) {
        helper.setText(R.id.tv_name, item.getNickName())
                .setText(R.id.tv_content, item.getBody())
                .setText(R.id.tv_time,item.getIntime());
        TextView tvPos = helper.getView(R.id.tv_pos);
        if (TextUtils.isEmpty(item.getAddress())) {
            tvPos.setVisibility(View.GONE);
        } else {
            tvPos.setVisibility(View.VISIBLE);
            tvPos.setText(item.getAddress());
        }
        ImageView imgHead = helper.getView(R.id.img_head);
        ImageView imgIsT = helper.getView(R.id.imd_teacher);

        imgIsT.setVisibility(item.isTeacher() ? View.VISIBLE : View.GONE);
        Glide.with(context).load(item.getHeadImg()).into(imgHead);
        helper.getView(R.id.img_comment).setOnClickListener(v -> {
            CommunityCommentPWindow communityCommentPWindow = new CommunityCommentPWindow(context,helper.getAdapterPosition(),item);
            communityCommentPWindow.setCallBack(callBack);
            communityCommentPWindow.show(v);
        });
        //填充图片数据
        RecyclerView recycImge = helper.getView(R.id.recycer_img);
        if (item.getImages() == null || item.getImages().size() == 0) {
            recycImge.setVisibility(View.GONE);
        } else {
            recycImge.setLayoutManager(new GridLayoutManager(context, item.getImages().size() == 1 ? 2 : 3));
            ChosePicAdapter commentAdapter = new ChosePicAdapter(item.getImages(), context, false);
            recycImge.setAdapter(commentAdapter);
            recycImge.setVisibility(View.VISIBLE);

        }
        //填充点赞数据
        RecyclerView recycLike = helper.getView(R.id.recycer_likr);
        if (item.getFabulousUser() == null || item.getFabulousUser().size() == 0) {
            recycLike.setVisibility(View.GONE);
        } else {
            recycLike.setLayoutManager(ChipsLayoutManager.newBuilder(context).build());
            LikePeopleAdapter likePeopleAdapter = new LikePeopleAdapter(item.getFabulousUser());
            recycLike.setAdapter(likePeopleAdapter);
            recycLike.setVisibility(View.VISIBLE);

        }
        //填充评论数据
        RecyclerView recycComment = helper.getView(R.id.recycer_comment);
        if (item.getComment() == null || item.getComment().size() == 0) {
            recycComment.setVisibility(View.GONE);
        } else {
            recycComment.setLayoutManager(new LinearLayoutManager(context));
            CommentAdapter commentAdapter = new CommentAdapter(item.getComment(),helper.getAdapterPosition());
            recycComment.setAdapter(commentAdapter);
            recycComment.setVisibility(View.VISIBLE);
        }
        //点赞和评论都没有，直接隐藏布局
        View commentAndLikeView = helper.getView(R.id.layout_botton);
        if (ListUtils.isEmpty(item.getComment()) && ListUtils.isEmpty(item.getFabulousUser()) ) {
            commentAndLikeView.setVisibility(View.GONE);
        } else {
            commentAndLikeView.setVisibility(View.VISIBLE);
        }

        //点赞评论同时存在，分割线才存在
        View likeLine = helper.getView(R.id.lin_dig);
        if (!ListUtils.isEmpty(item.getComment()) && !ListUtils.isEmpty( item.getFabulousUser())) {
            likeLine.setVisibility(View.VISIBLE);
        }else {
            likeLine.setVisibility(View.GONE);
        }
    }


    class CommentAdapter extends BaseQuickAdapter<CommunityEntity.Comment, BaseViewHolder> {
        int pos;
        public CommentAdapter(@Nullable List<CommunityEntity.Comment> data, int pos) {
            super(R.layout.item_community_comment, data);
            this.pos = pos;
        }

        @Override
        protected void convert(BaseViewHolder helper, CommunityEntity.Comment item) {
            helper.setText(R.id.tv_content, getStr(item));
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.comment(item,pos,2);

                }
            });

        }

        private SpannableString getStr(CommunityEntity.Comment comment) {
            SpannableStringBuilder builder = new SpannableStringBuilder();
            //一级回复
            if (comment.getType() == 1) {
                SpannableString spannableString = new SpannableString(comment.getNickName() + ":" + comment.getComment());
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0056A0"));
                spannableString.setSpan(colorSpan, 0, comment.getNickName().length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                return spannableString;
            }
            //二级回复
            else {
                SpannableString spannableString = new SpannableString(comment.getNickName() + "回复" + comment.getCommentUser() + ":" + comment.getComment());
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0056A0"));
                spannableString.setSpan(colorSpan, 0, comment.getNickName().length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(Color.parseColor("#0056A0"));
                spannableString.setSpan(colorSpan2, comment.getNickName().length() + 2, comment.getNickName().length() + 3 + comment.getCommentUser().length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                return spannableString;

            }


        }
    }
    public  interface CallBack{
        /**
         * 点赞
         * @param id
         */
        void fabulous(int id,int pos);

        /**
         * 评论
         * @param comment
         */
        void comment(CommunityEntity.Comment comment,int pos,int type);
    }
}
